Skip to content

gh-659: release the global migration lock when the apply fails - #663

Merged
jeremydmiller merged 1 commit into
masterfrom
gh-659-release-global-lock-on-failure
Sep 29, 2026
Merged

jeremydmiller merged 1 commit into
masterfrom
gh-659-release-global-lock-on-failure

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Closes #659.

The bug

DatabaseBase.ApplyAllConfiguredChangesToDatabaseAsync released the global migration lock only on its success paths. An exception anywhere between attaining the lock and finishing — initializeSchema, SchemaMigration.DetermineAsync, Migrator.ApplyAllAsync — skipped the release, and the finally only disposed the connection.

Every other store or replica sharing that database was then locked out of migrating, reporting Unable to attain the global lock in time to apply database changes — whose own advice, ResourceMigrationFailureMode.ContinueOnFailures, would start them against an unmigrated schema.

Disposing the connection is not enough, and the test proves it rather than arguing it: another_node_can_attain_the_lock_after_a_failed_apply fails on master. DISCARD ALL does release session advisory locks, but Npgsql resets a pooled connection when it is next used, not when it is returned, so the lock outlives the apply for as long as that connection sits idle in the pool. The repo already knew this in another corner — AdvisoryLockTesting.IsFreeAsync carries the comment "Pooled connections keep session locks after Close, so release explicitly".

The fix

The first of the issue's two suggestions: release on the way out, guarded on having actually attained the lock. Not pg_advisory_xact_lock — the apply is not bracketed in one transaction, so moving the lock's lifetime to a transaction would change where it is held, and this is a bug fix.

Two details worth calling out:

  • The release passes CancellationToken.None. A cancelled apply is one of the ways to reach this path, and the caller's token would mean never releasing in exactly that case.
  • A release that itself throws is swallowed. The migration failure is the one the operator needs to see, and a release that cannot run has nothing to fall back on — the connection is disposed either way.

The success paths keep their existing explicit releases and their exact current semantics, so a release failure there still surfaces as it does today.

Tests

releasing_the_global_lock in Weasel.Postgresql.Tests, over a real session-scoped advisory lock on an id of its own:

test on master
the_lock_is_released_after_a_successful_apply passes
the_lock_is_released_after_a_failed_apply fails
another_node_can_attain_the_lock_after_a_failed_apply fails
a_release_that_fails_does_not_replace_the_migration_failure passes (vacuously — nothing released)

Full local run against PostgreSQL 17: Weasel.Postgresql.Tests 1059 passed, Weasel.Core.Tests 574 passed.

Provenance

Found while investigating JasperFx/marten#5529: one test's host migration failed with a DDL error, the lock was never released, and every later test that started a host against that database failed to migrate. In a suite that reads as flakiness; in a deployment it is one node's failed migration bricking the rest. A stranded lock currently produces no error at all on the node that stranded it, which is why it went unnoticed.

🤖 Generated with Claude Code

ApplyAllConfiguredChangesToDatabaseAsync released the lock only on its success paths.
An exception anywhere between attaining it and finishing -- initializeSchema,
SchemaMigration.DetermineAsync, Migrator.ApplyAllAsync -- skipped the release, and the
finally block only disposed the connection.

Disposing is not enough. The lock is session-scoped, and while DISCARD ALL does release
session advisory locks, Npgsql resets a pooled connection when it is next used rather
than when it is returned, so the lock outlives the apply for as long as that connection
sits idle. Every other store or replica sharing the database is then locked out of
migrating, told "Unable to attain the global lock in time to apply database changes" --
advice whose own remedy, ContinueOnFailures, would start them against an unmigrated schema.

Release it on the way out instead, guarded on having actually attained it, and swallow a
release that itself fails so the migration failure stays the exception the caller sees.
The release passes CancellationToken.None: a cancelled apply is one of the ways to reach
this, and the caller's token would mean never releasing in exactly that case.

Found while investigating JasperFx/marten#5529, where one test's failed host migration
stranded the lock and every later test that migrated against that database failed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jeremydmiller
jeremydmiller merged commit d8b394e into master Sep 29, 2026
18 checks passed
@jeremydmiller
jeremydmiller deleted the gh-659-release-global-lock-on-failure branch September 29, 2026 19:56
@jeremydmiller jeremydmiller mentioned this pull request Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ApplyAllConfiguredChangesToDatabaseAsync never releases the global lock when the migration fails, locking every other node out of migrating

1 participant